package lang.reflect.Proxy;
import java.lang.reflect.Proxy;

/**
 * @author 黄军艳
 * @version 1.0
 * @date 2024/11/29  9:36
 */
public class LogTest {
    public static void main(String[] args) {
        //创建原始对象
        LogService logService=new LogServiceImpl();

        //创建代理对象
        LogService proxyInstance=(LogService) Proxy.newProxyInstance(logService.getClass().getClassLoader(),
                logService.getClass().getInterfaces(),new LoggingInvocationHandler(logService));

        //调用方法
        proxyInstance.testMrthod1();
        proxyInstance.testMrthod2();
        proxyInstance.testMrthod3();
    }
}

//定义服务接口
interface LogService{
    void testMrthod1();
    void testMrthod2();
    void testMrthod3();
}

//实现服务接口
class LogServiceImpl implements LogService{
    @Log
    public void testMrthod1() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("Executing testMrthod1");
    }

    @Log
    public void testMrthod2() {
        try {
            Thread.sleep(150);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("Executing testMrthod2");
    }

    @Override
    public void testMrthod3() {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("Executing testMrthod3");
    }
}
